/**
 * 
 */
package com.jm.web.builder.table;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.utils.Utils;
import com.jm.web.builder.BaseBuilder;
import com.jm.web.data.BaseDataEntity;
import com.jm.web.data.ReturnData;
import com.jm.web.form.table.Tr;
import com.jm.web.tags.support.BaseTag;
import com.jm.web.tags.table.TrTag;

/**
 * @author yanjie
 * Jan 30, 2012 2:35:10 PM
 */
public class TrBuilder extends BaseBuilder {
	TrTag trTag = null;
	Utils utils = null;
	ReturnData ret = null;
	String tableid ="";
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		trTag = (TrTag)this.getBaseTag();
		
		utils = trTag.getPage().getUtils();
		ret = utils.getReturnData();
		
		if(trTag.getTbodyTag() != null && ret != null && ret.getTableRowIndex() == 0 && ret.getTableRow() != null){
			tableid=trTag.getTbodyTag().getTableTag().getId();
			trTag.getTbodyTag().getTableTag().SetExtValue("auto",true);
			if(trTag.getTbodyTag().getTableTag().GetExtValue("stripe",true)){
				trTag.getTbodyTag().getTableTag().SetExtValue("stripe",false);//关闭旧的stripe
				trTag.getTbodyTag().getTableTag().SetExtValue("stripe1",true);//启用新的stripe
			}
		}
	}
	
	@Override
	public void RenderStart() {
		try {
			StringBuilderEx sb = new StringBuilderEx();
			sb.Append("<tr");
			
			//处理隔行变色
			if(trTag.getTbodyTag() != null && !this.trTag.getTbodyTag().isAutoMerge()
					&& trTag.getTbodyTag().getTableTag().GetExtValue("auto",false)
					&& trTag.getTbodyTag().getTableTag().GetExtValue("stripe1",false)){
				sb.Append(" class = \"%1$s %2$s\"",
						ret.getTableRowIndex()%2 == 0 ? "tr-darkstrip" : "tr-lightstrip",
						trTag.GetExtValue("class",""));
			}else if (!StringHelper.IsNullOrEmpty(trTag.GetExtValue("class",""))){
				sb.Append(" class = \"%1$s\"",
						trTag.GetExtValue("class",""));
			}
			
			if (!StringHelper.IsNullOrEmpty(trTag.GetExtValue("style",""))){
				sb.Append(" style = \"%1$s\"",
						trTag.GetExtValue("style",""));
			}
			
			if (!StringHelper.IsNullOrEmpty(trTag.GetExtValue("dirty",""))){
				sb.Append(" dirty = \"%1$s\"",
						trTag.GetExtValue("dirty",""));
			}
			
			if (!StringHelper.IsNullOrEmpty(trTag.GetExtValue("extAtr",""))){
				sb.Append(" %1$s",
						trTag.GetExtValue("extAtr",""));
			}
			
			if (!StringHelper.IsNullOrEmpty(trTag.GetExtValue("id",""))){
				sb.Append(" id = \"%1$s\"",
						trTag.GetExtValue("id",""));
			}
			
			if (!StringHelper.IsNullOrEmpty(trTag.GetExtValue("onclick",""))){
				sb.Append(" onclick = \"%1$s\"",
						trTag.GetExtValue("onclick",""));
			}
			if (!StringHelper.IsNullOrEmpty(trTag.GetExtValue("ondblclick",""))){
				sb.Append(" ondblclick = \"%1$s\"",
						trTag.GetExtValue("ondblclick",""));
			}
			
			if(!StringHelper.IsNullOrEmpty(trTag.GetExtValue("exportconfig",""))){
				sb.Append(" exportconfig = \"%1$s\"",
						trTag.GetExtValue("exportconfig",""));
			}
			
				
			
			if(this.trTag.getTbodyTag().isAutoMerge()){
				this.trTag.getTbodyTag().getTdIndex().SetParamValue("emptygroup",this.trTag.getTbodyTag().getTdIndex().gpiv("emptygroup",-1)+1);
				
				//處理合并單元格
				String groupValue =  utils.getGroupValue(this.trTag.getTbodyTag().getGroup(),ret.getTableRow());
				this.trTag.getTbodyTag().setGroupValue(groupValue);
				String lastGroupValue = this.trTag.getTbodyTag().getLastGroupValue();
				
				ret.setGroupValue("basegroup", groupValue);//每一行tr 放入group value
				ret.setPreGroupValue("basegroup", lastGroupValue);//每一行tr 放入group value
				
				if(!StringHelper.IsNullOrEmpty(lastGroupValue) && !lastGroupValue.equalsIgnoreCase(groupValue)){
//					Tr tr = new Tr();
//					tr.setTrBeginHtml("<tr><td colspan=\"17\">sss</td>");
//					tr.setTrEndHtml("</tr>");
//					this.trTag.getTbodyTag().getTrList().add(tr);
//					System.out.println("out tr");
					ReturnData ret = this.trTag.getPage().getReturnData(); 
					ret.setGroupDisplay("basegroup", true);
					
					this.trTag.getTbodyTag().outHtml(this.getOut());
					this.trTag.getTbodyTag().setLastGroupValue("");
					this.trTag.getTbodyTag().setTrList(new ArrayList());
					this.trTag.getTbodyTag().setLastTdMap(new HashMap());
					this.trTag.getTbodyTag().getTdIndex().SetParamValue("basegroup",
							this.trTag.getTbodyTag().getTdIndex().gpiv("basegroup")+1);
					
					ret.getTabledata().spiv("basegroup",ret.getTabledata().gpiv("basegroup")+1);
				}
				//處理合并2层及2层以上的合并
				if(this.trTag.getTbodyTag().isAutoMerge2()){
					BaseDataEntity groups = this.trTag.getTbodyTag().getSubGroups();
					if(groups != null){
						Enumeration keys = groups.getParamList().keys();
						while (keys.hasMoreElements())
						{
							String key = (String)keys.nextElement();
							
							if(key != null){
								key = ((String)key).toLowerCase();
								String subGroup = groups.gpsv(key);
								String subGroupValue =  utils.getGroupValue(subGroup,ret.getTableRow());
								this.trTag.getTbodyTag().getGroupValueMap().put(key,subGroupValue);
								String lastSubGroupValue = this.trTag.getTbodyTag().getLastGroupValue(key);
								
								if(!StringHelper.IsNullOrEmpty(lastSubGroupValue) && !lastSubGroupValue.equalsIgnoreCase(subGroupValue)){
									if(this.trTag.getTbodyTag().isSubtotal()){
//										int lev = Integer.parseInt(key.replace("group",""));
//										this.trTag.getTbodyTag().reCalculateRowspan(lev);//重計算單元格合并
//										Tr tr = new Tr();
//										tr.setTrBeginHtml("<tr><td>小計</td><td colspan=\"15\">sub</td>");
//										tr.setTrEndHtml("</tr>");
//										this.trTag.getTbodyTag().getTrList().add(tr);
									}
									
									this.trTag.getTbodyTag().getTdIndex().SetParamValue(key,this.trTag.getTbodyTag().getTdIndex().gpiv(key)+1);
									this.trTag.getTbodyTag().getLastGroupValueMap().remove(key);
								}
							}
						}
					}
				}
				
				Tr tr = new Tr();
				tr.setTrBeginHtml(sb.toString());
				this.trTag.getTbodyTag().getTrList().add(tr);
			}else{
				sb.Append(">");
				this.getOut().write(sb.toString());
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void RenderEnd() {
		try {
			if(this.trTag.getTbodyTag().isAutoMerge()){
				this.trTag.getTbodyTag().setLastGroupValue(this.trTag.getTbodyTag().getGroupValue());
				if(this.trTag.getTbodyTag().isAutoMerge2()){
					BaseDataEntity groups = this.trTag.getTbodyTag().getSubGroups();
					if(groups != null){
						Enumeration keys = groups.getParamList().keys();
						while (keys.hasMoreElements())
						{
							String key = (String)keys.nextElement();
							if(key != null){
								 key = ((String)key).toLowerCase();
								 this.trTag.getTbodyTag().getLastGroupValueMap().put(key,this.trTag.getTbodyTag().getGroupValue(key));
							}
						}
					}
				}
				
				this.trTag.getTbodyTag().getLastTr().setTrEndHtml("</tr>");
				
				if(ret.isLastRecord()){
//					Tr tr = new Tr();
//					tr.setTrBeginHtml("<tr><td colspan=\"17\">sss</td>");
//					tr.setTrEndHtml("</tr>");
//					this.trTag.getTbodyTag().getTrList().add(tr);
					ReturnData ret = this.trTag.getPage().getReturnData();
					ret.setGroupDisplay("basegroup", true);
					//ret.setGroupValue("basegroup", this.trTag.getTbodyTag().getGroupValue());
					
					this.trTag.getTbodyTag().outHtml(this.getOut());
					this.trTag.getTbodyTag().setLastGroupValue("");
					this.trTag.getTbodyTag().setTrList(null);
					this.trTag.getTbodyTag().setLastTdMap(null);
					this.trTag.getTbodyTag().setGroupValueMap(null);
					this.trTag.getTbodyTag().setLastGroupValueMap(null);
					
					ret.getTabledata().spiv("basegroup",ret.getTabledata().gpiv("basegroup")+1);
				}
			}else{
				this.getOut().write("</tr>");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
